<template>
  <div style="width: 500px;">
    <el-input
        placeholder="请输入部门名称"
        v-model="filterText"
        prefix-icon="el-icon-search">
    </el-input>

    <el-tree
        class="filter-tree"
        :data="deps"
        :props="defaultProps"
        :filter-node-method="filterNode"
        :expand-on-click-node="false"
        ref="tree">
      <div class="custom-tree-node" slot-scope="{ node, data }" style="display: flex;justify-content: space-between; width: 100%;">
        <span style="font-size: 13px">部门名称</span>
        <el-tag type="success">{{ data.name }}</el-tag>
        <span>是否启用</span>
        <el-tag type="warning">{{ data.enabled?'是':'否' }}</el-tag>
        <span>
          <el-button
              type="primary"
              size="mini"
              class="depBtn"
              icon="el-icon-plus"
              @click="() => showAddDep(data)">
<!--            添加部门-->
          </el-button>
          <el-button
              type="danger"
              size="mini"
              class="depBtn"
              icon="el-icon-delete"
              @click="() => deleteDep(data)">
<!--            删除部门-->
          </el-button>
        </span>
      </div>
    </el-tree>
    <el-dialog
        title="添加部门"
        :visible.sync="dialogVisible"
        width="30%">
      <div>
        <table>
          <tr>
            <td>
              <el-tag>上级部门</el-tag>
            </td>
            <td>
              <span>{{pname}}</span>
            </td>
          </tr>
          <tr>
            <td>
              <el-tag>部门名称</el-tag>
            </td>
            <td>
              <el-input v-model="dep.name" placeholder="请输入部门名称" />
            </td>
          </tr>
        </table>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addDep">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "DepMana",
  data(){
    return{
      filterText:'',
      deps: [],
      defaultProps:{
        children: 'children',
        label: 'name'
      },
      dialogVisible: false,
      dep:{
        name: '',
        parentId:''
      },
      pname:''
    }
  },
  methods:{
    filterNode(value, data) {
      if (!value) return true;
      return data.name.indexOf(value) !== -1;
    },
    initDep(){
      this.dep = {
        name: '',
        parentId: -1
      }
      this.pname = ''
    },
    initDeps(){
      this.getRequest('/system/basic/department/').then(resp =>{
        if (resp){
          this.deps = resp;
        }
      })
    },
    showAddDep(data){
      this.dialogVisible = true;
      this.dep.parentId = data.id;
      this.pname = data.name
    },
    deleteDep(data){
      this.$confirm('此操作将永久删除['+data.name+']部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteRequest('/system/basic/department/'+data.id).then(resp =>{
          if (resp){
            this.initDeps();
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    addDep(){
      this.postRequest('/system/basic/department/',this.dep).then(resp =>{
        if (resp){
          this.dialogVisible = false;
          this.initDep()
          this.initDeps();
        }
      })
    },

  },
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    this.initDeps();
  }
}
</script>

<style scoped>

.depBtn{
  padding: 2px;
}

</style>